/*
 * Copyright 2012 Marsrutai.lt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.activity;

import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.activity.intent.BookmarkIntentHolder;
import lt.marsrutai.mobile.android.adapter.TabAdapter;
import lt.marsrutai.mobile.android.adapter.TabInfoProvider;
import lt.marsrutai.mobile.android.fragment.BookmarksFragment.ActionModeManager;
import lt.marsrutai.mobile.android.fragment.StopBookmarksFragment;
import lt.marsrutai.mobile.android.fragment.TrackBookmarksFragment;
import lt.marsrutai.mobile.android.fragment.TransportStopBookmarksFragment;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.ActionMode;
import com.viewpagerindicator.TabPageIndicator;

public class BookmarkActivity extends BaseActivity implements ActionModeManager {
    
    private ViewPager viewPager;
    
    /** Temp. variable. Used for saving currently active tab index */ 
    private int tabIndex = 0;

    /**
     * Manages the action mode - finishes it if tab change occurs
     */
    private ActionMode actionMode;
    
    @Override
    protected void setupActionBar(final ActionBar actionBar) {
        super.setupActionBar(actionBar);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle(getString(R.string.bookmarks));
    }

    @Override
    protected int getNavigationMode() {
        return ActionBar.NAVIGATION_MODE_STANDARD;
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        restoreState(savedInstanceState);
        
        initialize();
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        
        if (viewPager != null) {
            tabIndex = viewPager.getCurrentItem();
        }
    }
    
    @Override
    protected void onResume() {
        super.onResume();

        if (viewPager != null) {
            viewPager.setCurrentItem(tabIndex);
        }
    }

    private void restoreState(Bundle bundle) {
        if (bundle == null) {
            bundle = getIntent().getExtras();
        }

        if (bundle != null) {
            BookmarkIntentHolder intentHolder = new BookmarkIntentHolder(bundle);
            tabIndex = intentHolder.getTabIndex();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        BookmarkIntentHolder intentHolder = new BookmarkIntentHolder(new Bundle());
        
        intentHolder.setTabIndex(viewPager.getCurrentItem());
        
        outState.putAll(intentHolder.getIntent().getExtras());
        
        super.onSaveInstanceState(outState);
    }

    /**
     * Creates tabs for each schedule group and initializes the adapter
     */
    private void initialize() {
        TabAdapter adapter = new TabAdapter(getSupportFragmentManager());

        adapter.addTab(new TabInfoProvider(StopBookmarksFragment.class, 
                null, getString(R.string.stops)));
        adapter.addTab(new TabInfoProvider(TransportStopBookmarksFragment.class, 
                null, getString(R.string.transport_stops)));
        adapter.addTab(new TabInfoProvider(TrackBookmarksFragment.class, 
                null, getString(R.string.tracks_short)));

        // Attach the adapted to the view pager
        setContentView(R.layout.tabs_viewpager_layout);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(adapter);
        // Attach view pager to tab page indicator
        TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
        indicator.setOnPageChangeListener(new OnPageChangeListener() {
            
            @Override
            public void onPageSelected(int position) {
                if (actionMode != null) {
                    actionMode.finish();
                    actionMode = null;
                }
            }
            
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        indicator.setViewPager(viewPager);

        if (tabIndex >= 0) {
            viewPager.setCurrentItem(tabIndex);
        }
    }

    @Override
    public void setActionMode(ActionMode actionMode) {
        this.actionMode = actionMode;
    }

    @Override
    public ActionMode getActionMode() {
        return actionMode;
    }
}
